<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Quotation extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		$this->load->database();
		$this->load->helper('url');

		$this->load->library('grocery_CRUD');
	}
	public function _example_output($output = null)
	{
		$this->load->view('master/grid',$output);
	}
	// public function io()
	// {
	// 	$this->load->vars('title','Quotation / QL Scope of Work');

	// 	$crud = new grocery_CRUD();
		
	// 	$crud->set_theme('flexigrid');
	// 	$this->config->load('grocery_crud');
	// 	$crud->set_table('ql_scope_of_work');
	// 	$crud->set_subject('QL Scope of Work');
	// 	// $crud->unset_fields('userpassword');
	// 	// $crud->unset_columns('userpassword');
	// 	$crud->field_type('attachment','true_false');
	// 	$crud->display_as('attachment','Attach');

	// 	$crud->display_as('name','Keterangan');
	// 	$crud->display_as('ordering','No. Urut');
	// 	$crud->field_type('name','text');
	// 	$crud->unset_texteditor('name');
	// 	$crud->unset_export();
	// 	$crud->unset_print();
	// 	// $crud->set_relation('id_cabang','cabang','nama');
	// 	// $crud->display_as('id_cabang','Cabang');
	// 	//$output->title = ;
	// 	$output = $crud->render();

	// 	$this->_example_output($output);
	// }
	public function scope()
	{
		$this->load->vars('title','Quotation / QL Scope of Work');

		$crud = new grocery_CRUD();
		
		$crud->set_theme('flexigrid');
		$this->config->load('grocery_crud');
		$crud->set_table('ql_scope_of_work');
		$crud->set_subject('QL Scope of Work');
		// $crud->unset_fields('userpassword');
		// $crud->unset_columns('userpassword');
		$crud->field_type('attachment','true_false');
		$crud->display_as('attachment','Attach');

		$crud->display_as('name','Keterangan');
		$crud->display_as('ordering','No. Urut');
		$crud->field_type('name','text');
		$crud->unset_texteditor('name');
		$crud->unset_export();
		$crud->unset_print();
	
		$output = $crud->render();

		$this->_example_output($output);
	}
	public function supported_doc()
	{
		$this->load->vars('title','Quotation / Supported Document Needed For COI');

		$crud = new grocery_CRUD();
		
		$crud->set_theme('flexigrid');
		$this->config->load('grocery_crud');
		$crud->set_table('ql_supported_doc');
		$crud->set_subject('Supported Document Needed For COI');
		// $crud->unset_fields('userpassword');
		// $crud->unset_columns('userpassword');
		// $crud->field_type('attachment','true_false');
		// $crud->display_as('attachment','Attach');

		$crud->display_as('name','Keterangan');
		$crud->display_as('ordering','No. Urut');
		$crud->field_type('name','text');
		$crud->unset_texteditor('name');

		$crud->unset_export();
		$crud->unset_print();
		// $crud->set_relation('id_cabang','cabang','nama');
		// $crud->display_as('id_cabang','Cabang');
		//$output->title = ;
		$output = $crud->render();

		$this->_example_output($output);
	}
	public function letter()
	{
		/*
		"id", "no_surat", "tgl_surat", "Importir", "Alamat_Importir",
 		"Fax_Importir", "ql_attn", "ql_dear", "ql_subject", "ql_no", "ql_date"

 		"Tanggal_Periksa", "Alamat_Pemeriksaan", "ql_price", "ql_vat", "ql_total", "ql_charge", "idr_bank", 
 		"idr_bank_addr", "idr_bank_ac", "usd_bank", "usd_bank_addr", "usd_bank_ac", "from_name", "from_fax", 
 		"from_email", "ql_tertanda", "ql_jabatan", "ql_jab_init1", "ql_jab_init2"
		*/
		$this->load->vars('title','Quotation');

		

		$crud = new grocery_CRUD();
		$crud->columns('no_surat','tgl_surat','ql_subject','ql_no','ql_date','Alamat_Importir');
		$crud->set_relation('id_afiliasi','cabang','nama');
		$crud->display_as('no_surat','Nomor Surat');
		$crud->display_as('tgl_surat','Tanggal');
		$crud->display_as('id_afiliasi','Cabang Afiliasi');
		$crud->display_as('Importir','Nama Perusahaan');
		$crud->display_as('Alamat_Importir','Alamat');
		$crud->display_as('Fax_Importir','Fax');
		$crud->display_as('from_fax','Fax');
		$crud->display_as('from_email','Email');
		$crud->display_as('ql_attn','Attn');
		$crud->display_as('ql_dear','Kepada (nama)');
		$crud->display_as('ql_subject','Subject');
		$crud->display_as('ql_no','No');
		$crud->display_as('ql_date','Tanggal');
		$crud->display_as('Tanggal_Periksa','Tanggal Pemeriksaan');
		$crud->display_as('Alamat_Pemeriksaan','Tempat Pemeriksaan');

		$crud->display_as('ql_price_curr','Inspection Fee');
		$crud->display_as('ql_price','');
		$crud->display_as('ql_charge_curr','Charge');
		$crud->display_as('ql_charge','');
		$crud->display_as('ql_total','Total');
		$crud->display_as('ql_vat','VAT (10%)');
		$crud->display_as('ql_price','');

		$crud->display_as('idr_bank','Nama Bank');
		$crud->display_as('idr_bank_addr','Alamat');
		$crud->display_as('idr_bank_ac','No. Rekening');
		$crud->display_as('usd_bank','Nama Bank');
		$crud->display_as('usd_bank_addr','Alamat');
		$crud->display_as('usd_bank_ac','No. Rekening');

		$crud->display_as('from_name','Nama Perusahaan');
		$crud->display_as('ql_jabatan','Jabatan');
		$crud->display_as('ql_tertanda','Nama Pejabat');
		$crud->display_as('ql_jab_init1','Inisial');
		$crud->display_as('ql_jab_init2','');
		//$crud->display_as('ql_price','');

		$crud->set_theme('flexigrid');
		$this->config->load('grocery_crud');
		$crud->set_table('quotation');
		$crud->set_subject('Quotation');

		$crud->callback_after_insert(array($this, '_add_ceklist'));
		$crud->callback_after_update(array($this, '_edit_ceklist'));

		$crud->unset_export();
		$crud->unset_print();
		$crud->unset_read();

		$crud->add_action('', 'pdf', 'quotation/pdf/ql','ui-icon ui-icon-disk qlt_pdf_cmd');

		$output = $crud->render();

		$this->_example_output($output);
	}
	
	public function _edit_ceklist($post,$id)
	{
		$ql_scope_of_work = json_decode($post['ql_scope_of_work']);
		foreach ($ql_scope_of_work as $scope_id => $value ) {
			$this->db->where('id',$scope_id)->update('ql_scope_of_work_ceklist',array('value'=>$value));
		}
		$ql_supported_doc = json_decode($post['ql_supported_doc']);
		foreach ($ql_supported_doc as $scope_id => $value ) {
			$this->db->where('id',$scope_id)->update('ql_supported_doc_ceklist',array('value'=>$value));
		}
	}
	
	public function _add_ceklist($post,$id)
	{
		$ql_scope_of_work = json_decode($post['ql_scope_of_work']);
		foreach ($ql_scope_of_work as $scope_id => $value ) {
			$this->db->insert('ql_scope_of_work_ceklist',array('quotation_id'=>$id,'ql_scope_of_work_id'=>$scope_id,'value'=>$value));
		}
		$ql_supported_doc = json_decode($post['ql_supported_doc']);
		foreach ($ql_supported_doc as $scope_id => $value ) {
			$this->db->insert('ql_supported_doc_ceklist',array('quotation_id'=>$id,'ql_supported_doc_id'=>$scope_id,'value'=>$value));
		}
	}

	public function form_ceklist_scope_of_work($quotation_id,$ret=FALSE)
	{
		if( $quotation_id == 'insert')
		{
			$ql_scope_of_work = $this->db->order_by('ordering')->get('ql_scope_of_work')->result_object();
			$scope_count = count($ql_scope_of_work);
			$data = '';
			$data .= '<div class="form-input-box">';
			foreach ($ql_scope_of_work as $ceklist) {
				$data .= '<div class="scope"><input class="set" type="checkbox" rowid="'.$ceklist->id.'" '. ($ceklist->attachment == '1' ? ' checked="checked" ':' '). '/> <label>'.$ceklist->name.'</label></div>';
			}
			$data .= '</div><input type="hidden" name="ql_scope_of_work"/><div class="clear"></div><br/>';
			//print_r($data);
			if($ret)
				return $data;
			else
				echo $data;
			die();
		}
		$io = $this->db->where('id',$quotation_id)->get('quotation')->row();
		if(!empty($io))
		{
			$ql_scope_of_work = $this->db->order_by('ordering')->get('ql_scope_of_work')->result_object();
			$scope_count = count($ql_scope_of_work);

			$ql_scope_of_work_ceklist = $this->db->select('a.id,a.value,b.name,b.id as scope_id')
													 ->where('a.quotation_id',$quotation_id)
													 ->from('ql_scope_of_work_ceklist a')
													 ->join('ql_scope_of_work b','a.ql_scope_of_work_id = b.id')
													 ->get()->result_object();
			//										 
			$ceklist_count = count($ql_scope_of_work_ceklist);
			
			//die();
			//echo $ceklist_count;
			if($scope_count != $ceklist_count)
			{
				//echo 'You Are Here';
				//$ql_scope_of_work_ceklist = array();
				//$this->db->where('quotation_id',$quotation_id)->delete('ql_scope_of_work_ceklist');
				// GET ID SCOPE OF WORK FOR CHECKING UNEXISTENT
				$must_exists = array();
				$already_exists = array();
				foreach ($ql_scope_of_work as $scope_of_work) {
					$must_exists[] = $scope_of_work->id;

				}
				foreach ($ql_scope_of_work_ceklist as $ceklist ) {
					$already_exists[]=$ceklist->scope_id;
				}	
				$unexistent = array_diff($must_exists, $already_exists);
				
				//print_r($unexistent);
				//die();
				if(count($unexistent) > 0)
				{
					foreach ($unexistent as $scope_id) {
						$ceklist = array(
							'quotation_id'	=> $quotation_id,
							'ql_scope_of_work_id' => $scope_id,
							'value' => ''	
						);
						$this->db->insert('ql_scope_of_work_ceklist',$ceklist);
					}
					
				}

					
				// AMBIL KEMBALI
				$ql_scope_of_work_ceklist = $this->db->select('a.id,a.value,b.name')
													 ->where('a.quotation_id',$quotation_id)
													 ->from('ql_scope_of_work_ceklist a')
													 ->join('ql_scope_of_work b','a.ql_scope_of_work_id = b.id')
													 ->get()->result_object();
				$ceklist_count = count($ql_scope_of_work_ceklist);
			}
		//	$data  = '<div class="form-field-box even io-scope-of-work">';
			$data = '';
			$data .= '<div class="form-input-box">';
			foreach ($ql_scope_of_work_ceklist as $ceklist) {
				$data .= '<div class="scope"><input class="set" type="checkbox" rowid="'.$ceklist->id.'" '. ($ceklist->value == '1' ? ' checked="checked" ':' '). '/> <label>'.$ceklist->name.'</label></div>';
			}
			$data .= '</div><input type="hidden" name="ql_scope_of_work"/><div class="clear"></div><br/>';
			//print_r($data);

			if($ret)
				return $data;
			else
				echo $data;
		}
	}
	public function form_ceklist_supported_doc($quotation_id,$ret=FALSE)
	{
		if( $quotation_id == 'insert')
		{
			$ql_supported_doc = $this->db->order_by('ordering')->get('ql_supported_doc')->result_object();
			$scope_count = count($ql_supported_doc);
			$data = '';
			$data .= '<div class="form-input-box">';
			foreach ($ql_supported_doc as $ceklist) {
				$data .= '<div class="supported_doc"><input class="set" type="checkbox" rowid="'.$ceklist->id.'"/> <label>'.$ceklist->name.'</label></div>';
			}
			$data .= '</div><input type="hidden" name="ql_supported_doc"/><div class="clear"></div><br/>';
			//print_r($data);
			if($ret)
				return $data;
			else
				echo $data;
			die();
		}
		$io = $this->db->where('id',$quotation_id)->get('quotation')->row();
		if(!empty($io))
		{
			$ql_supported_doc = $this->db->order_by('ordering')->get('ql_supported_doc')->result_object();
			$scope_count = count($ql_supported_doc);

			$ql_supported_doc_ceklist = $this->db->select('a.id,a.value,b.name,b.id as scope_id')
													 ->where('a.quotation_id',$quotation_id)
													 ->from('ql_supported_doc_ceklist a')
													 ->join('ql_supported_doc b','a.ql_supported_doc_id = b.id')
													 ->get()->result_object();
			//										 
			$ceklist_count = count($ql_supported_doc_ceklist);
			
			//die();
			//echo $ceklist_count;
			if($scope_count != $ceklist_count)
			{
				//echo 'You Are Here';
				//$ql_supported_doc_ceklist = array();
				//$this->db->where('quotation_id',$quotation_id)->delete('ql_supported_doc_ceklist');
				// GET ID SCOPE OF WORK FOR CHECKING UNEXISTENT
				$must_exists = array();
				$already_exists = array();
				foreach ($ql_supported_doc as $supported_doc) {
					$must_exists[] = $supported_doc->id;

				}
				foreach ($ql_supported_doc_ceklist as $ceklist ) {
					$already_exists[]=$ceklist->scope_id;
				}	
				$unexistent = array_diff($must_exists, $already_exists);
				
				//print_r($unexistent);
				//die();
				if(count($unexistent) > 0)
				{
					foreach ($unexistent as $scope_id) {
						$ceklist = array(
							'quotation_id'	=> $quotation_id,
							'ql_supported_doc_id' => $scope_id,
							'value' => ''	
						);
						$this->db->insert('ql_supported_doc_ceklist',$ceklist);
					}
					
				}

					
				// AMBIL KEMBALI
				$ql_supported_doc_ceklist = $this->db->select('a.id,a.value,b.name')
													 ->where('a.quotation_id',$quotation_id)
													 ->from('ql_supported_doc_ceklist a')
													 ->join('ql_supported_doc b','a.ql_supported_doc_id = b.id')
													 ->get()->result_object();
				$ceklist_count = count($ql_supported_doc_ceklist);
			}
		//	$data  = '<div class="form-field-box even io-scope-of-work">';
			$data = '';
			$data .= '<div class="form-input-box">';
			foreach ($ql_supported_doc_ceklist as $ceklist) {
				$data .= '<div class="supported_doc"><input class="set" type="checkbox" rowid="'.$ceklist->id.'" '. ($ceklist->value == '1' ? ' checked="checked" ':' '). '/> <label>'.$ceklist->name.'</label></div>';
			}
			$data .= '</div><input type="hidden" name="ql_supported_doc"/><div class="clear"></div><br/>';
			//print_r($data);

			if($ret)
				return $data;
			else
				echo $data;
		}
	}  
	public function form_ceklist($quotation_id)
	{
		$data = array(
			'scope' => $this->form_ceklist_scope_of_work($quotation_id,1),
			'supported_doc' => $this->form_ceklist_supported_doc($quotation_id,1)
		);

		echo json_encode($data);	
	}

	public function request()
	{
		$this->load->vars('title','Request For Quotation');

		$crud = new grocery_CRUD();
		

		$crud->set_theme('flexigrid');
		$this->config->load('grocery_crud');
		

		$crud->set_table('request_for_quotation');

		$crud->display_as('roq_no','Nomor Surat');
		$crud->display_as('roq_date','Tanggal Surat');
		$crud->display_as('roq_to','Kepada');
		$crud->display_as('roq_from','Dari');

		$crud->set_subject('Request For Quotation');
		$crud->set_relation('id_afiliasi','cabang','nama');

		$crud->columns('roq_no','roq_date','roq_to','roq_from','subject');

		$crud->field_type('to_address','text');
		$crud->unset_texteditor('to_address');
		$crud->field_type('from_address','text');
		$crud->unset_texteditor('from_address');
		$crud->field_type('exp_address','text');
		$crud->unset_texteditor('exp_address');
		$crud->field_type('imp_address','text');
		$crud->unset_texteditor('imp_address');

		$crud->callback_add_field('subject_oi',array($this,'_cb_subject_oi'));
		$crud->callback_edit_field('subject_oi',array($this,'_cb_subject_oi'));
		$crud->callback_add_field('description_og',array($this,'_cb_description_og'));
		$crud->callback_edit_field('description_og',array($this,'_cb_description_og'));
		//$crud->unset_texteditor('name');
		$crud->unset_export();
		$crud->unset_print();
		$crud->unset_read();
		$crud->add_action('', 'pdf', 'quotation/pdf/rfq','ui-icon ui-icon-disk rfq_pdf_cmd');

		$output = $crud->render();

		$this->_example_output($output);
	}
	public function _cb_subject_oi($val,$id)
	{
		return '<div id="subject_oi_field_box"> <div id="field-subject_oi"> <input type="radio" name="subject_oi" value="1" '.($val=='1'?' checked' : '').' />  Second Hand Good    <input type="radio" name="subject_oi" value="2" '.($val=='2'?' checked' : '').'/> Others</div></div>';
	}
	public function _cb_description_og($val,$id)
	{
		return '<div id="description_og_field_box"> <div id="field-description_og" style="width:400px !important"><input type="radio" name="description_og" value="1" '.($val=='1'?' checked' : '').' />  Machine/Heavy Equipment    <input type="radio" name="description_og" value="2" '.($val=='2'?' checked' : '').'/> Others</div></div>';
	}

	////
	public function pdf($what,$id)
	{
		if( !in_array($what, array('rfq','ql')) || !$id )
			die('Invalid Parameter');
		$tbl = array(
			'rfq' => 'request_for_quotation',
			'ql' => 'quotation'
		);
		$ql = $this->db->where('id',$id)->get($tbl[$what])->row();
		$config = $this->db->get('config')->row();
		if($ql){
			//// PDF STAERTED
			// As PDF creation takes a bit of memory, we're saving the created file in /downloads/reports/
			$filename = $tbl[$what] . '-'.$id;
			$pdfFilePath = FCPATH."/dl/pdfs/$filename.pdf";

			//die($pdfFilePath);
			$data['page_title'] = 'Hello world'; // pass data to the view
			$data['ql'] = $ql; 
			$data['config'] = $config;
			if($what == 'ql'){
				//////////AWALAN
				if(!empty($ql))
				{
					$quotation_id = $id;
					$ql_supported_doc = $this->db->order_by('ordering')->get('ql_supported_doc')->result_object();
					$scope_count = count($ql_supported_doc);

					$ql_supported_doc_ceklist = $this->db->select('a.id,a.value,b.name,b.id as scope_id')
															 ->where('a.quotation_id',$quotation_id)
															 ->from('ql_supported_doc_ceklist a')
															 ->join('ql_supported_doc b','a.ql_supported_doc_id = b.id')
															 ->get()->result_object();
					//										 
					$ceklist_count = count($ql_supported_doc_ceklist);
					
					//die();
					//echo $ceklist_count;
					if($scope_count != $ceklist_count)
					{
						//echo 'You Are Here';
						//$ql_supported_doc_ceklist = array();
						//$this->db->where('quotation_id',$quotation_id)->delete('ql_supported_doc_ceklist');
						// GET ID SCOPE OF WORK FOR CHECKING UNEXISTENT
						$must_exists = array();
						$already_exists = array();
						foreach ($ql_supported_doc as $supported_doc) {
							$must_exists[] = $supported_doc->id;

						}
						foreach ($ql_supported_doc_ceklist as $ceklist ) {
							$already_exists[]=$ceklist->scope_id;
						}	
						$unexistent = array_diff($must_exists, $already_exists);
						
						//print_r($unexistent);
						//die();
						if(count($unexistent) > 0)
						{
							foreach ($unexistent as $scope_id) {
								$ceklist = array(
									'quotation_id'	=> $quotation_id,
									'ql_supported_doc_id' => $scope_id,
									'value' => ''	
								);
								$this->db->insert('ql_supported_doc_ceklist',$ceklist);
							}
							
						}

							
						
					}
					// AMBIL KEMBALI
					$data['supported_doc'] = $this->db->select('a.id,a.value,b.name')
														 ->where('a.value','1')	
														 ->where('a.quotation_id',$quotation_id)
														 ->from('ql_supported_doc_ceklist a')
														 ->join('ql_supported_doc b','a.ql_supported_doc_id = b.id')
														 ->get()->result_object();
					$data['supported_doc_count'] = count($data['supported_doc']);

					// if( $data['supported_doc_count'] % 2 != 0 ){
					// 	$data['supported_doc'][] = 'E';
					// 	$data['supported_doc_count'] += 1;
					// }
				}
				/////////////////////////AKHIRAN
				/*BEGIN*/	
				if(!empty($ql))
				{
					$quotation_id = $id;
					$ql_scope_of_work = $this->db->order_by('ordering')->get('ql_scope_of_work')->result_object();
					$scope_count = count($ql_scope_of_work);

					$ql_scope_of_work_ceklist = $this->db->select('a.id,a.value,b.name,b.id as scope_id')
															 ->where('a.quotation_id',$quotation_id)
															 ->from('ql_scope_of_work_ceklist a')
															 ->join('ql_scope_of_work b','a.ql_scope_of_work_id = b.id')
															 ->get()->result_object();
					//										 
					$ceklist_count = count($ql_scope_of_work_ceklist);
					
					//die();
					//echo $ceklist_count;
					if($scope_count != $ceklist_count)
					{
						//echo 'You Are Here';
						//$ql_scope_of_work_ceklist = array();
						//$this->db->where('quotation_id',$quotation_id)->delete('ql_scope_of_work_ceklist');
						// GET ID SCOPE OF WORK FOR CHECKING UNEXISTENT
						$must_exists = array();
						$already_exists = array();
						foreach ($ql_scope_of_work as $scope_of_work) {
							$must_exists[] = $scope_of_work->id;

						}
						foreach ($ql_scope_of_work_ceklist as $ceklist ) {
							$already_exists[]=$ceklist->scope_id;
						}	
						$unexistent = array_diff($must_exists, $already_exists);
						
						//print_r($unexistent);
						//die();
						if(count($unexistent) > 0)
						{
							foreach ($unexistent as $scope_id) {
								$ceklist = array(
									'quotation_id'	=> $quotation_id,
									'ql_scope_of_work_id' => $scope_id,
									'value' => ''	
								);
								$this->db->insert('ql_scope_of_work_ceklist',$ceklist);
							}
							
						}
					}
				}
					
				// AMBIL KEMBALI
				$data['scope_of_work'] = $this->db->select('a.id,a.value,b.name')
													 ->where('a.value','1')	
													 ->where('a.quotation_id',$quotation_id)
													 ->from('ql_scope_of_work_ceklist a')
													 ->join('ql_scope_of_work b','a.ql_scope_of_work_id = b.id')
													 ->get()->result_object();
				$data['scope_of_work_count'] = count($data['scope_of_work']);
				/*END*/ 
				

				// if( $data['scope_of_work_count'] % 2 != 0 ){
				// 	$data['scope_of_work'][] = 'E';
				// 	$data['scope_of_work_count'] += 1;
				// }
				//$data['scope_max_table_row'] = ceil($data['scope_of_work_count'] / 2);

			}else{
				////
				if(!empty($ql))
				{
					$quotation_id = $id;
					$ql_scope_of_work = $this->db->order_by('ordering')->get('ql_scope_of_work')->result_object();
					$scope_count = count($ql_scope_of_work);

					$ql_scope_of_work_ceklist = $this->db->select('a.id,a.value,b.name,b.id as scope_id')
															 ->where('a.quotation_id',$quotation_id)
															 ->from('ql_scope_of_work_ceklist a')
															 ->join('ql_scope_of_work b','a.ql_scope_of_work_id = b.id')
															 ->get()->result_object();
					//										 
					$ceklist_count = count($ql_scope_of_work_ceklist);
					
					//die();
					//echo $ceklist_count;
					if($scope_count != $ceklist_count)
					{
						//echo 'You Are Here';
						//$ql_scope_of_work_ceklist = array();
						//$this->db->where('quotation_id',$quotation_id)->delete('ql_scope_of_work_ceklist');
						// GET ID SCOPE OF WORK FOR CHECKING UNEXISTENT
						$must_exists = array();
						$already_exists = array();
						foreach ($ql_scope_of_work as $scope_of_work) {
							$must_exists[] = $scope_of_work->id;

						}
						foreach ($ql_scope_of_work_ceklist as $ceklist ) {
							$already_exists[]=$ceklist->scope_id;
						}	
						$unexistent = array_diff($must_exists, $already_exists);
						
						//print_r($unexistent);
						//die();
						if(count($unexistent) > 0)
						{
							foreach ($unexistent as $scope_id) {
								$ceklist = array(
									'quotation_id'	=> $quotation_id,
									'ql_scope_of_work_id' => $scope_id,
									'value' => ''	
								);
								$this->db->insert('ql_scope_of_work_ceklist',$ceklist);
							}
							
						}
					}

					
				}
				// AMBIL KEMBALI
				$data['scope_of_work'] = $this->db->select('a.id,a.value,b.name')
													 ->where('a.quotation_id',$quotation_id)
													 ->from('ql_scope_of_work_ceklist a')
													 ->join('ql_scope_of_work b','a.ql_scope_of_work_id = b.id')
													 ->get()->result_object();
				$date['scope_of_work_count'] = count($data['scope_of_work']);

				if( $data['scope_of_work_count'] % 2 != 0 ){
					$data['scope_of_work'][] = 'E';
					$data['scope_of_work_count'] += 1;
				}	
				////
			}
				//print_r($data);
				//die();
			    //ini_set('memory_limit','32M'); // boost the memory limit if it's low <img src="http://davidsimpson.me/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley">
			    $html = $this->load->view('pdfs/_'.$tbl[$what], $data, true); // render the view into HTML
			     
			    $this->load->library('pdf');
			    $pdf = $this->pdf->load();
			  
				$pdf->WriteHTML($html);

				$pdf->Output($filename.'.pdf','D');  
		
			//// PDF ENDED
		}
	}
}